﻿using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Reader.Libs.PRC;
using System;
using System.IO;
using System.Text;

namespace Reader.Libs.CONVERTER
{
    public class TxtConverter
    {
        public void ConvertToDoc(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatUnicodeText);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatDocument97, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToDocX(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatUnicodeText);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatDocumentDefault, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToHtml(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatUnicodeText);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatHTML, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToPdf(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatUnicodeText);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatPDF, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToMobi(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                objWord.Documents.Open(FileName: SourcePath, Format: WdOpenFormat.wdOpenFormatUnicodeText);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    if (!System.IO.Directory.Exists("C://tmp"))
                        System.IO.Directory.CreateDirectory("C://tmp");
                    else
                    {
                        foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                            f.Delete();
                        foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                        {
                            dir.Delete(true);
                        }
                    }
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: "C://tmp//tmp.html", FileFormat: WdSaveFormat.wdFormatHTML, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);

                    string result = "";
                    using (StreamReader reader = new StreamReader("C://tmp//tmp.html"))
                    {
                        result = reader.ReadToEnd();
                        reader.Dispose();
                    }
                    MobiDoc md = new MobiDoc();
                    md.BookContent = result;
                    md.writeContent(FileToSave);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }
    }
}
